package cn.zch.soft.util.img2hex;
/**
 * @class PicUtils
 * @description TODO
 * @author zch
 * @date 2019/7/24
 * @version V0.0.1.201907241503.01
 * @function
 * @modfiyDate
 * @createDate
 * @package
 */
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class ImgZoom {
	// private String destFile;
	private int width;
	private int height;
	private BufferedImage img;
	private String ext;
	/**
	 * 缩放图片工具的构造函数
	 * @param srcFile String srcFile
	 * @exception IOException IOException
	 */
	private ImgZoom(String srcFile) throws IOException {
		//得到最后一个.的位置
		int index = srcFile.lastIndexOf(".");
		//获取被缩放的图片的格式
		this.ext = srcFile.substring(index + 1);
		//获取目标路径(和原始图片路径相同,在文件名后添加了一个_s)
		// this.destFile = srcFile.substring(0, index) + getNowTime() + "_s." + ext;
		//读取图片,返回一个BufferedImage对象
		this.img = ImageIO.read(new File(srcFile));
		//获取图片的长和宽
		this.width = img.getWidth();
		this.height = img.getHeight();
	}
	/**
	 * 按比例对图片进行缩放.
	 * @param scale 缩放比例
	 * @exception IOException IOException
	 */
	private void zoomByScale(double scale, String destFile) throws IOException {
		//获取缩放后的长和宽
		int _width = (int) (scale * width);
		int _height = (int) (scale * height);
		//获取缩放后的Image对象
		Image _img = img.getScaledInstance(_width, _height, Image.SCALE_DEFAULT);
		//新建一个和Image对象相同大小的画布
		BufferedImage image = new BufferedImage(_width, _height, BufferedImage.TYPE_INT_RGB);
		//获取画笔
		Graphics2D graphics = image.createGraphics();
		//将Image对象画在画布上,最后一个参数,ImageObserver:接收有关 Image 信息通知的异步更新接口,没用到直接传空
		graphics.drawImage(_img, 0, 0, null);
		//释放资源
		graphics.dispose();
		//使用ImageIO的方法进行输出,记得关闭资源
		OutputStream out = new FileOutputStream(destFile);
		ImageIO.write(image, ext, out);
		out.close();
	}
	/**
	 * 指定长和宽对图片进行缩放
	 * @param width 长
	 * @param height 宽
	 * @exception IOException IOException
	 */
	private void zoomBySize(int width, int height, String destFile) throws IOException {
		//与按比例缩放的不同只在于,不需要获取新的长和宽,其余相同.
		Image image1 = img.getScaledInstance(width, height, Image.SCALE_DEFAULT);
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics2D graphics = image.createGraphics();
		graphics.drawImage(image1, 0, 0, null);
		graphics.dispose();
		OutputStream out = new FileOutputStream(destFile);
		ImageIO.write(image, ext, out);
		out.close();
	}
	/**
	 * 图片缩放 3
	 * @param srcImage 输入图
	 * @param result 输出图
	 * @param desWidth 宽度
	 * @param desHeight 高度
	 */
	public void imageResize(String srcImage, String result, int desWidth, int desHeight) {
		try {
			// 读入文件
			BufferedImage src = ImageIO.read(new File(srcImage));
			// 获取指定长宽的图像实例
			Image image = src.getScaledInstance(desHeight, desWidth, Image.SCALE_DEFAULT);
			// 缩放图像
			BufferedImage tag = new BufferedImage(desHeight, desWidth, BufferedImage.TYPE_INT_RGB);
			Graphics g = tag.getGraphics();
			// 绘制缩小后的图
			g.drawImage(image, 0, 0, null);
			g.dispose();
			// 输出到文件流
			ImageIO.write(tag, "png", new File(result));
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * main 测试
	 * @param args main
	 */
	public static void main(String[] args) {
		try {
			//			String srcFile = "E:\\T\\qr\\16658_320.bmp";
			//			int index = srcFile.lastIndexOf(".");
			//			//获取被缩放的图片的格式
			//			String ext = srcFile.substring(index + 1);
			//			String destFile = srcFile.substring(0, index) + "1_s." + ext;
			//			ImgZoom picUtils = new ImgZoom(srcFile);
			//			picUtils.zoomByScale(0.2, destFile);
			//			Thread.sleep(1000);
			//			destFile = srcFile.substring(0, index) + "2_s." + ext;
			//			picUtils.zoomBySize(64, 64, destFile);
			//			Thread.sleep(1000);
			//			destFile = srcFile.substring(0, index) + "3_s." + ext;
			//			picUtils.imageResize(srcFile,destFile,64,64);
			String srcFile = "E:\\zchhhh\\20190730193324_L.png";
			int index = srcFile.lastIndexOf(".");
			//获取被缩放的图片的格式
			String ext = srcFile.substring(index + 1);
			String destFile = srcFile.substring(0, index) + "1_s." + ext;
			ImgZoom picUtils = new ImgZoom(srcFile);
			destFile = srcFile.substring(0, index) + "ZOOM1." + ext;
			picUtils.zoomBySize(64, 64, destFile);
			Thread.sleep(1000);
			destFile = srcFile.substring(0, index) + "ZOOM2." + ext;
			picUtils.imageResize(srcFile, destFile, 64, 64);
		}
		catch (IOException | InterruptedException e) {
			e.printStackTrace();
		}
	}
	private static String getNowTime() {
		// return DateUtil.now().replace("-", "").replace(":", "").replace(" ", "")
		return String.valueOf(System.currentTimeMillis());
	}
}
